﻿using IceCS.EMS.BLL;
using System;
using System.Windows.Forms;

namespace IceCS.EMS.UI.WinFormCore
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }
        private void ShowFormAddDialog()
        {
            var formAdd = FormService.FormAdd;
            formAdd.ShowDialog();
        }
        private void ShowFormEditDialog()
        {
            //用于判断是否选中了DataGridView中的一行
            if (dataGridView1.SelectedRows.Count != 1)
            {
                MessageBox.Show("请选中一行进行操作");
                return;
            }
            var first = dataGridView1.SelectedRows[0];
            var employeeID = Convert.ToInt32(first.Cells["EmployeeID"].Value);
            var formEdit = FormService.FormEdit;
            formEdit.SetEditId(employeeID);
            formEdit.ShowDialog();
        }

        private void ShowFormDeleteDialog()
        {
            //用于判断是否选中了DataGridView中的一行
            if (dataGridView1.SelectedRows.Count != 1)
            {
                MessageBox.Show("请选中一行进行操作");
                return;
            }
            var dialogResult = MessageBox.Show("确定要删除此记录吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dialogResult == DialogResult.No)
            {
                return;
            }
            var first = dataGridView1.SelectedRows[0];
            var employeeID = Convert.ToInt32(first.Cells["EmployeeID"].Value); 
            FormService.EmployeeManager.DeleteEmployee(employeeID);
            ReloadGridData();
        }
        private void toolStripMenuItemAdd_Click(object sender, System.EventArgs e)
        {
            ShowFormAddDialog();
        }

        private void toolStripMenuItemEdit_Click(object sender, System.EventArgs e)
        {
            ShowFormEditDialog();
        }

        private void toolStripMenuItemDelete_Click(object sender, System.EventArgs e)
        {
            ShowFormDeleteDialog();
        }

        private void FormMain_Load(object sender, System.EventArgs e)
        {
            ReloadGridData();
        }

        
        private void toolStripMenuItemRefresh_Click(object sender, EventArgs e)
        {
            ReloadGridData();
        }

        public void ReloadGridData()
        {
            dataGridView1.DataSource = FormService.EmployeeManager.GetEmployeeModelList();
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            ShowFormAddDialog();
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            ShowFormEditDialog();
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            ShowFormDeleteDialog();
        }
    }
}
